package cn.zhouchao.org.service.impl;

import cn.zhouchao.basic.query.BaseQuery;
import cn.zhouchao.basic.service.impl.BaseServiceImpl;
import cn.zhouchao.basic.util.MD5Utils;
import cn.zhouchao.basic.util.PageList;
import cn.zhouchao.basic.util.StrUtils;
import cn.zhouchao.org.domain.Employee;
import cn.zhouchao.org.mapper.EmployeeMapper;
import cn.zhouchao.org.service.IEmployeeService;
import cn.zhouchao.system.mapper.RoleMapper;
import cn.zhouchao.system.vo.EmpAndRoleVo;
import cn.zhouchao.user.domain.Logininfo;
import cn.zhouchao.user.mapper.LogininfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private RoleMapper roleMapper;

//    public static void main(String[] args) {
//        Employee employee = new Employee();
//        String salt = StrUtils.getComplexRandomString(32);//生成32位的盐值
//        System.out.println("盐值："+salt);
//
//        employee.setSalt(salt);//设置该员工的盐值
//        employee.setPassword(salt + 1);//设置密码
//        String password = employee.getPassword();
//        String pwdtomd = MD5Utils.encrypByMd5(salt + password);
//        System.out.println("盐值+密："+pwdtomd);
//    }

    /**
     * 重写添加方法--完善权限所需
     * @param employee
     */
    @Override
    public void add(Employee employee) {
        //设置密码、
        String salt = StrUtils.getComplexRandomString(32);//生成32位的盐值
        employee.setSalt(salt);//设置该员工的盐值
        employee.setPassword(salt + 1);//设置密码
        //添加登录者信息
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);//将emp的数据复制给logininfo
        logininfo.setDisable(true);
        logininfo.setType(0);
        logininfoMapper.add(logininfo);
        //todo 添加emp时判断是否有带店铺id
        employee.setLogininfoId(logininfo.getId());
        super.add(employee);

    }

    /**
     * 重写修改方法--给员工修改角色时
     *
     * @param employee
     */
    @Override
    public void update(Employee employee) {
        super.update(employee);
        //删除
        roleMapper.deleteByEmpId(employee.getId());
        //添加
        roleMapper.addMiddle(employee.getId(),employee.getRoles());
    }

    /**
     * 重写分页查询业务
     * 将联表查询的数据装入分页查询的数据中，一次性返回给前端
     * @param Query
     * @return
     */
    @Override
    public PageList<Employee> queryByPage(BaseQuery Query) {
        PageList<Employee> pageList = super.queryByPage(Query);
        if (null == pageList || CollectionUtils.isEmpty(pageList.getRows())){
            return pageList;
        }
        // 将角色集合放入到每个人的对象下面
        List<Employee> emps = pageList.getRows();
        List<Long> empIds = emps.stream().map(Employee::getId).collect(Collectors.toList()); //将所有数据遍历，取员工对象Id装成Ids集合
        // 根据empIds集合获取到所有的角色
        List<EmpAndRoleVo> EmpAndRoles = roleMapper.findByEmpIds(empIds);
        // 将 empAndRoleList 转成 map key：empid  value：List<Long> roleId
        Map<Long, List<Long>> map = EmpAndRoles.stream().collect(
                Collectors.groupingBy(
                        EmpAndRoleVo::getEmployeeId, Collectors.mapping(EmpAndRoleVo::getRoleId, Collectors.toList())));
        //遍历emps 将roleId集合放入到emp对象中
        emps.forEach(emp->{
            List<Long> roleIds = map.get(emp.getId());
            emp.setRoles(roleIds);
        });
        return pageList;
    }
}
